package com.sky.controller.admin;

import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.service.DishesService;
import com.sky.service.SetmealService;
import com.sky.result.Result;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐对应的增删改查
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api("套餐")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    private DishesService dishesService;

    @PostMapping
    @ApiOperation("增加套餐")
    @CachePut(cacheNames = "setmealCache", key = "#setmealVO.categoryId")
    public Result SetmeaInsert(@RequestBody SetmealVO setmealVO){

        log.info("增加套餐:{}", setmealVO);
        setmealService.setmealInsert(setmealVO);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<SetmealVO> SetmeaById(@PathVariable Long id){

        log.info("根据id查询:{}", id);
        SetmealVO  SetmealVO = setmealService.SetmeaById(id);

        return Result.success(SetmealVO);
    }

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> pageList(SetmealPageQueryDTO setmealPageQueryDTO){

        log.info("套餐分页查询:{}", setmealPageQueryDTO);

        PageResult pageResult = setmealService.pageList(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    @PutMapping
    @ApiOperation("修改数据")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealVO.categoryId")
    public Result setmeaUpdate(@RequestBody SetmealVO setmealVO){

        log.info("修改数据:{}", setmealVO);
        setmealService.setmeaUpdate(setmealVO);

        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id批量删除")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result setmeaDelete(@RequestParam List<Long> ids){

        log.info("根据id批量删除:{}", ids);
        setmealService.setmeaDelete(ids);

        return Result.success();
    }

    /**
     * 套餐的起售和禁售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐的起售和禁售")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result setmealStatus(@PathVariable Integer status, Long id){

        log.info("套餐的起售和禁售:{}", status, id);

        setmealService.setmealStatus(status, id);
        return Result.success();
    }
}
